/*
 * @Author: Wanko
 * @Date: 2023-09-15 13:43:53
 * @LastEditors: Wanko
 * @LastEditTime: 2024-03-01 15:14:09
 * @Description:
 */
const Koa = require('koa')
const app = new Koa()
const koaStatic = require('koa-static')
const { koaBody } = require('koa-body')
const path = require('path')
const router = require('./router')
const error = require('koa-json-error')
require('./db')
const debug = require('./env')

const {fileDir} = require('./config')
console.log(fileDir)
console.log(debug)
app.use(
  koaStatic('public', {
    extension: false // 禁用自动扩展名匹配
  })
)
app.use(
  koaBody({
    multipart: true,
    formidable: {
      uploadDir: fileDir,
      keepExtensions: true,
      onFileBegin: function (name, file) {
        // 重命名文件为原始文件名
        // const fileName = file.originalFilename
        // .replaceAll(' ', '_')
        // .replace(/[`~!@#$%^&*()|\-=?;:'",<>\{\}\\\/]/gi, '_')
        // file.newFilename = fileName
        // 覆盖文件存放的完整路径(保留原始名称)
        // file.filepath = `${fileDir}/${fileName}`
        return true
      }
    }
  })
)
router(app)

app.use(
  error({
    postFormat: (e, { stack, status: code, ...rest }) => {
      return {
        code,
        ...rest
      }
    }
  })
)

app.listen(3001, () => {
  console.log('static 应用已启动在端口 3001')
})
